|
1
|
|
|
/* xlsx.js (C) 2013-present SheetJS -- http://sheetjs.com */ |
|
2
|
|
|
/* vim: set ts=2: */ |
|
3
|
|
|
import { Component } from '@angular/core'; |
|
4
|
|
|
|
|
5
|
|
|
import * as XLSX from 'xlsx'; |
|
6
|
|
|
|
|
7
|
|
|
import { File } from '@ionic-native/file'; |
|
8
|
|
|
|
|
9
|
|
|
type AOA = any[][]; |
|
10
|
|
|
|
|
11
|
|
|
@Component({ |
|
12
|
|
|
selector: 'page-home', |
|
13
|
|
|
template: ` |
|
14
|
|
|
<ion-header><ion-navbar><ion-title>SheetJS Ionic Demo</ion-title></ion-navbar></ion-header> |
|
15
|
|
|
|
|
16
|
|
|
<ion-content padding> |
|
17
|
|
|
<ion-grid> |
|
18
|
|
|
<ion-row *ngFor="let row of data"> |
|
19
|
|
|
<ion-col *ngFor="let val of row"> |
|
20
|
|
|
{{val}} |
|
21
|
|
|
</ion-col> |
|
22
|
|
|
</ion-row> |
|
23
|
|
|
</ion-grid> |
|
24
|
|
|
</ion-content> |
|
25
|
|
|
|
|
26
|
|
|
<ion-footer> |
|
27
|
|
|
<input type="file" (change)="onFileChange($event)" multiple="false" /> |
|
28
|
|
|
<button ion-button color="secondary" (click)="import()">Import Data</button> |
|
29
|
|
|
<button ion-button color="secondary" (click)="export()">Export Data</button> |
|
30
|
|
|
</ion-footer> |
|
31
|
|
|
` |
|
32
|
|
|
}) |
|
33
|
|
|
|
|
34
|
|
|
export class HomePage { |
|
35
|
|
|
data: any[][] = [[1,2,3],[4,5,6]]; |
|
36
|
|
|
constructor(public file: File) {}; |
|
37
|
|
|
|
|
38
|
|
|
read(bstr: string) { |
|
39
|
|
|
/* read workbook */ |
|
40
|
|
|
const wb: XLSX.WorkBook = XLSX.read(bstr, {type: 'binary'}); |
|
41
|
|
|
|
|
42
|
|
|
/* grab first sheet */ |
|
43
|
|
|
const wsname: string = wb.SheetNames[0]; |
|
44
|
|
|
const ws: XLSX.WorkSheet = wb.Sheets[wsname]; |
|
45
|
|
|
|
|
46
|
|
|
/* save data */ |
|
47
|
|
|
this.data = <AOA>(XLSX.utils.sheet_to_json(ws, {header: 1})); |
|
48
|
|
|
}; |
|
49
|
|
|
|
|
50
|
|
|
write(): XLSX.WorkBook { |
|
51
|
|
|
/* generate worksheet */ |
|
52
|
|
|
const ws: XLSX.WorkSheet = XLSX.utils.aoa_to_sheet(this.data); |
|
53
|
|
|
|
|
54
|
|
|
/* generate workbook and add the worksheet */ |
|
55
|
|
|
const wb: XLSX.WorkBook = XLSX.utils.book_new(); |
|
56
|
|
|
XLSX.utils.book_append_sheet(wb, ws, 'SheetJS'); |
|
57
|
|
|
|
|
58
|
|
|
return wb; |
|
59
|
|
|
}; |
|
60
|
|
|
|
|
61
|
|
|
/* File Input element for browser */ |
|
62
|
|
|
onFileChange(evt: any) { |
|
63
|
|
|
/* wire up file reader */ |
|
64
|
|
|
const target: DataTransfer = <DataTransfer>(evt.target); |
|
65
|
|
|
if (target.files.length !== 1) throw new Error('Cannot use multiple files'); |
|
66
|
|
|
const reader: FileReader = new FileReader(); |
|
67
|
|
|
reader.onload = (e: any) => { |
|
68
|
|
|
const bstr: string = e.target.result; |
|
69
|
|
|
this.read(bstr); |
|
70
|
|
|
}; |
|
71
|
|
|
reader.readAsBinaryString(target.files[0]); |
|
72
|
|
|
}; |
|
73
|
|
|
|
|
74
|
|
|
/* Import button for mobile */ |
|
75
|
|
|
async import() { |
|
76
|
|
|
try { |
|
77
|
|
|
const target: string = this.file.documentsDirectory || this.file.externalDataDirectory || this.file.dataDirectory || ''; |
|
78
|
|
|
const dentry = await this.file.resolveDirectoryUrl(target); |
|
79
|
|
|
const url: string = dentry.nativeURL || ''; |
|
80
|
|
|
alert(`Attempting to read SheetJSIonic.xlsx from ${url}`) |
|
81
|
|
|
const bstr: string = await this.file.readAsBinaryString(url, "SheetJSIonic.xlsx"); |
|
82
|
|
|
this.read(bstr); |
|
83
|
|
|
} catch(e) { |
|
84
|
|
|
const m: string = e.message; |
|
85
|
|
|
alert(m.match(/It was determined/) ? "Use File Input control" : `Error: ${m}`); |
|
86
|
|
|
} |
|
87
|
|
|
}; |
|
88
|
|
|
|
|
89
|
|
|
/* Export button */ |
|
90
|
|
|
async export() { |
|
91
|
|
|
const wb: XLSX.WorkBook = this.write(); |
|
92
|
|
|
const filename: string = "SheetJSIonic.xlsx"; |
|
93
|
|
|
try { |
|
94
|
|
|
/* generate Blob */ |
|
95
|
|
|
const wbout: ArrayBuffer = XLSX.write(wb, { bookType: 'xlsx', type: 'array' }); |
|
96
|
|
|
const blob: Blob = new Blob([wbout], {type: 'application/octet-stream'}); |
|
97
|
|
|
|
|
98
|
|
|
/* find appropriate path for mobile */ |
|
99
|
|
|
const target: string = this.file.documentsDirectory || this.file.externalDataDirectory || this.file.dataDirectory || ''; |
|
100
|
|
|
const dentry = await this.file.resolveDirectoryUrl(target); |
|
101
|
|
|
const url: string = dentry.nativeURL || ''; |
|
102
|
|
|
|
|
103
|
|
|
/* attempt to save blob to file */ |
|
104
|
|
|
await this.file.writeFile(url, filename, blob, {replace: true}); |
|
105
|
|
|
alert(`Wrote to SheetJSIonic.xlsx in ${url}`); |
|
106
|
|
|
} catch(e) { |
|
107
|
|
|
if(e.message.match(/It was determined/)) { |
|
108
|
|
|
/* in the browser, use writeFile */ |
|
109
|
|
|
XLSX.writeFile(wb, filename); |
|
110
|
|
|
} |
|
111
|
|
|
else alert(`Error: ${e.message}`); |
|
112
|
|
|
} |
|
113
|
|
|
}; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
|